package com.engine.cese.duss.srm.taskpool.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.srm.util.GenUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-09
 * @Description: 物料采购任务池查询列表
 * @ModifyLog:
 **/

public class WeaTaskPoolAction {

    /**
     * @Description: 任务回退
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/backTaskData")
    @Produces({MediaType.TEXT_PLAIN})
    public String backTaskData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String ids = Util.null2String( request.getParameter( "ids" ) );
            String cglx = Util.null2String( request.getParameter( "cglx" ) );
            RecordSet rs = new RecordSet();
            rs.writeLog( "-------------------------" + ids );
            RecordSet rs1 = new RecordSet();
            String cgxqTab = GenUtil.getCgxqTableType( cglx );
            String cgrwTab = GenUtil.getCgrwTableType( cglx );
            //清空对应需求的“采购员”为空
            String sql = "select id,xqdid from " + cgrwTab + " where id in (" + ids + ")";
            rs.executeQuery( sql );
            while (rs.next()) {
                String mainid = Util.null2String( rs.getString( "id" ) );
                String xqdid = Util.null2String( rs.getString( "xqdid" ) );
                rs.executeUpdate( "update " + cgxqTab + " set cgy='',fplx=0 where id=?", xqdid );
                String sql2 = "select xqmxid from " + cgrwTab + "_dt1 where mainid=?";
                rs1.executeQuery( sql2, mainid );
                while (rs1.next()) {
                    String xqmxid = Util.null2String( rs1.getString( "xqmxid" ) );
                    String sql3 = "update " + cgxqTab + "_dt1 set cfbj=0 where mainid=? and id in (" + xqmxid + ")";
                    rs1.executeUpdate( sql3, xqdid );
                }

            }
            //删除所选的任务
            String sql1 = "delete from " + cgrwTab + " where id in (" + ids + ")";
            if (rs.executeUpdate( sql1 )) {
                String sql12 = "delete from " + cgrwTab + "_dt1 where mainid in (" + ids + ")";
                rs.executeUpdate( sql12 );
            }

            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 招标采购立项合并多任务
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/zblxMergeTask")
    @Produces({MediaType.TEXT_PLAIN})
    public String zblxMergeTask(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String ids = Util.null2String( request.getParameter( "ids" ) );
            String cglx = Util.null2String( request.getParameter( "cglx" ) );
            RecordSet rs = new RecordSet();
            rs.writeLog( "----------------zblxMergeTask---------" + ids );
            String cgrwTab = GenUtil.getCgrwTableType( cglx );
            rs.executeQuery( "select wm_concat(id) from uf_cgrw_wl where concat(xmmc,xqbh) \n" +
                    "in (select concat(xmmc,xqbh) from uf_cgrw_wl where id in (" + ids + ") " +
                    "group by concat(xmmc,xqbh) having count(concat(xmmc,xqbh)) > 1)\n" +
                    "and id in (" + ids + ") " );
            rs.next();
            String newids = Util.null2String( rs.getString( 1 ) );
            if (!"".equals( newids )) {
                Map<String, String> resultMap = GenUtil.getRwdmxids( newids );
                String rwmxids = resultMap.get( "rwmxids" );
                String minRwdidId = resultMap.get( "mainid" );
                String sql = "select wm_concat(xqmxid) as xqmxid,wlbm,wlmc," +
                        "cms,xlh,xlbz,dw,sum(sl) as sl,fzdw,sum(fzsl) as fzsl,\n" +
                        "sum(cgsl) as cgsl,sum(sysl) as sysl,fbbh,zfbbh,zfbid,wlid\n" +
                        "from " + cgrwTab + "_dt1 where mainid in(" + newids + ")\n" +
                        "group by wlbm,wlmc,cms,xlh,xlbz,dw,fzdw,fbbh,zfbbh,zfbid,wlid";
                rs.executeQuery( sql );
                while (rs.next()) {
                    String wlbm = Util.null2String( rs.getString( "wlbm" ) );//物料编码
                    String wlmc = Util.null2String( rs.getString( "wlmc" ) );//物料名称
                    String cms = Util.null2String( rs.getString( "cms" ) );//长描述
                    String xlh = Util.null2String( rs.getString( "xlh" ) );//序列号
                    String xlbz = Util.null2String( rs.getString( "xlbz" ) );//序列备注
                    String dw = Util.null2String( rs.getString( "dw" ) );//单位
                    String sl = Util.null2String( rs.getString( "sl" ) );//数量
                    String fzdw = Util.null2String( rs.getString( "fzdw" ) );//辅助单位
                    String fzsl = Util.null2String( rs.getString( "fzsl" ) );//辅助数量
                    String cgsl = Util.null2String( rs.getString( "cgsl" ) );//采购数量
                    String sysl = Util.null2String( rs.getString( "sysl" ) );//剩余数量
                    String fbbh = Util.null2String( rs.getString( "fbbh" ) );//分部编号
                    String zfbbh = Util.null2String( rs.getString( "zfbbh" ) );//子分部编号
                    String zfbid = Util.null2String( rs.getString( "zfbid" ) );//子分部id
                    String wlid = Util.null2String( rs.getString( "wlid" ) );//物料id
                    String xqmxid = Util.null2String( rs.getString( "xqmxid" ) );//需求明细id
                    String sql1 = "insert into " + cgrwTab + "_dt1(mainid,wlbm,wlmc,cms,xlh,xlbz,dw,sl,fzdw," +
                            "fzsl,cgsl,sysl,fbbh,zfbbh,zfbid,wlid,xqmxid) " +
                            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    Object[] params = {minRwdidId, wlbm, wlmc, cms, xlh, xlbz, dw, sl, fzdw,
                            fzsl, cgsl, sysl, fbbh, zfbbh, zfbid, wlid, xqmxid};
                    rs.executeUpdate( sql1, params );
                }
                rs.executeUpdate( "delete from " + cgrwTab + "_dt1 where id in (" + rwmxids + ")" );
            }
            String rwdids = GenUtil.getCgRwdids( ids );
            apidatas.put( "datas", rwdids );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
}
